# -*- coding: utf-8 -*-
import subprocess
import schedule
import time
import datetime
from multiprocessing import Process
from scrapy import cmdline
import logging


def crawl_work():
    # subprocess.Popen('scrapy crawl it')
    print('-'*100)
    # args = ["scrapy", "crawl", 'it']
    # while True:
    #     start = time.time()
    #     p = Process(target=cmdline.execute, args=(args,))
    #     p.start()
    #     p.join()
    #     logging.debug("### use time: %s" % (time.time() - start))
def schedule01():
    print('*'*10+'开始执行定时爬虫'+'*'*10)
    schedule.every(1).minutes.do(crawl_work)
    print('当前时间为{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    print('*' * 10 + '定时爬虫开始运行' + '*' * 10)
    while True:
        schedule.run_pending()
        time.sleep(10)

# 配置参数即可, 爬虫名称，运行频率
confs = [
    {
        "spider_name": "it",
        "frequency": 2,
    },
]

def start_spider(spider_name, frequency):
    args = ["scrapy", "crawl", spider_name]
    while True:
        start = time.time()
        p = Process(target=cmdline.execute, args=(args,))
        p.start()
        p.join()
        logging.debug("### use time: %s" % (time.time() - start))
        time.sleep(frequency)

def schedule02():
    for conf in confs:
        process = Process(target=start_spider, args=(conf["spider_name"], conf["frequency"]))
        process.start()
        time.sleep(86400)

def schedule3():
    import croniter
    import time
    from datetime import datetime

    cron = croniter.croniter("0 10 * * *")  # 每天 10:00

    while True:
        next_time = cron.get_next(datetime)
        sleep_time = (next_time - datetime.now()).total_seconds()
        time.sleep(sleep_time)
        print("Task executed")

def main_schedule(day_run_time,callback_fun,sch_type=None):
    import schedule
    import time

    if sch_type == "S":
        schedule.every(day_run_time).seconds.do(callback_fun)
    elif sch_type == "M":
        schedule.every(day_run_time).minutes.do(callback_fun)
    elif sch_type == "H":
        schedule.every(day_run_time).hour.do(callback_fun)
    else:
        # 每天 10:00 执行任务
        schedule.every().day.at(day_run_time).do(callback_fun)
    # schedule.every().friday.at()

    while True:
        schedule.run_pending()
        time.sleep(1)

# schedule 库支持多种时间间隔设置，例如：
# 每 10 秒：schedule.every(10).seconds.do(job)
# 每 1 小时：schedule.every().hour.do(job)
# 每天 10 点：schedule.every().day.at("10:00").do(job)
# 设置每 10 分钟运行一次任务
# schedule.every(10).minutes.do(job)

def main_schedule_minutes(interval,callback_fun):
    import schedule
    import time

    def task():
        print("Task executed")

    # 每天 10:00 执行任务
    schedule.every().minutes.do(callback_fun)

    # schedule.every().friday.at()

    while True:
        schedule.run_pending()
        time.sleep(1)



if __name__ == '__main__':
    # schedule4()
    pass